package com.leoman.controller.backend;

import com.leoman.common.DataTableFactory;
import com.leoman.controller.common.CommonController;
import com.leoman.core.Constant;
import com.leoman.entity.Circle;
import com.leoman.service.CircleService;
import com.leoman.service.CommentService;
import com.leoman.service.PraiseService;
import com.leoman.utils.DateUtils;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 草丛管理
 * Created by 涂奕恒 on 2016/12/6.
 */
@Controller
@RequestMapping(value = "backend/circle")
public class CircleController extends CommonController {

    @Autowired
    private CircleService circleService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private PraiseService praiseService;

    @RequestMapping("index")
    public String index() {
        return "backend/草丛列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     String userName,
                     String startTime,
                     String endTime,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<Circle> page = circleService.pageByParams(userName, startTime, endTime, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("add")
    public String add(ModelMap model, Integer circleId) {
        if (null != circleId) {
            Circle circle = circleService.getById(circleId);
            circle.setContent(circle.getContent().replaceAll("&lt", "<").replaceAll("&gt", ">"));
            circle.setCommentNum(commentService.findListBySourceIdAndType(circleId, Constant.SOURCE_TYPE_CIRCLE).size());
            circle.setPraiseNum(praiseService.findListBySourceIdAndType(circleId, Constant.SOURCE_TYPE_CIRCLE).size());
            model.addAttribute("circle", circle);
            model.addAttribute("createTime", DateUtils.longToStringWithFormat(circle.getCreateTime(), "yyyy-MM-dd hh:mm:ss"));

            return "backend/草丛详情";
        } else {
            return "backend/添加草丛";
        }
    }

    /**
     * 置顶草丛
     *
     * @param circleId 草丛ID
     */
    @RequestMapping("/toTop")
    @ResponseBody
    public Integer toTop(Integer circleId) {
        try {
            return circleService.toTop(circleId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 添加or修改草丛
     */
    @RequestMapping("/save")
    @ResponseBody
    public Integer save(String title, String txtVal, String content, String labels, MultipartRequest multipartRequest) {
        try {
            circleService.addCircleInfo(true, null, null, title, content, txtVal, labels, Constant.USER_STATUS_NO, multipartRequest);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除草丛
     *
     * @param circleId 草丛ID
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(Integer circleId) {
        try {
            circleService.deleteById(circleId);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 批量删除草丛
     *
     * @param ids 草丛ID
     * @return
     */
    @RequestMapping("/batchDelete")
    @ResponseBody
    public Integer batchDelete(String ids) {
        try {
            return circleService.batchDelete(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
